#include <pthread.h>

#include <iostream>
// #include <stdlib.h>
#include <unistd.h>

#include <string>

using namespace std;

static char *str = nullptr;
static pthread_mutex_t lock;

void *handler(void *arg) {
  // 锁定
  pthread_mutex_lock(&lock);
  str = (char *)arg;
  sleep(0.5);
  // 解锁
  pthread_mutex_unlock(&lock);
  return nullptr;
}

int main(int argc, char const *argv[]) {
  // 初始化互斥锁
  pthread_mutex_init(&lock, NULL);

  pthread_t p1, p2;
  pthread_create(&p1, NULL, handler, (void *)"HELLO");
  pthread_create(&p2, NULL, handler, (void *)"WORLD");

  pthread_join(p1, NULL);
  cout << str << endl;
  pthread_join(p2, NULL);
  cout << str << endl;

  // 销毁互斥锁
  pthread_mutex_destroy(&lock);
  return 0;
}
